package com.example.graduationdesignbackend.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.Product;
import com.example.graduationdesignbackend.query.ProductQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuze
 * @since 2024-05-04
 */
@RestController
@SaCheckLogin
@SaCheckRole("admin")
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping()
    Res<Product> getProductInfo(String id){
        return Res.yuze(productService.getById(id));
    }

    @GetMapping("/myPage")
    Res<PageDto<Product>> getMyProductPage(ProductQuery productQuery){
        return Res.yuze(productService.getMyProduct(productQuery));
    }

    @GetMapping("/page")
    Res<PageDto<Product>> getProductPage(ProductQuery productQuery){
        return Res.yuze(productService.getProductPage(productQuery));
    }

    @PostMapping
    Res createProduct(@RequestBody Product product){
        productService.createProduct(product);
        return Res.yuze();
    }

    @PutMapping
    Res editProduct(@RequestBody Product product){
        return productService.editProduct(product);
    }

    @PutMapping("/listing")
    Res listingProducts(@RequestBody Product dto){return productService.listingProducts(dto.getId());}

    @PutMapping("/delisting")
    Res delistingProduct(@RequestBody Product dto){return productService.delistingProducts(dto.getId());}

    @DeleteMapping
    Res deleteProduct(String id){
        Boolean delete = productService.removeById(id);
        if (!delete)return Res.fail(ResponseCode.Fail);
        return Res.yuze();
    }
}
